package com.aitoedu.server.config;

import cn.dev33.satoken.stp.StpInterface;
import com.aitoedu.server.mapper.UserRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Slf4j
public class StpInterfaceImplConfig implements StpInterface {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<String> getPermissionList(Object o, String s) {
        Long userId = Long.valueOf((String) o);
        List<String> permission = new ArrayList<>();

        return null;
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
        Long userId = Long.valueOf((String) o);
        List<String> role = (List<String>)redisTemplate.opsForValue().get("RoleList:"+(String) o);
        if(role!=null) return role;
        role = userRoleMapper.getAllRoleByUserId(userId);
        redisTemplate.opsForValue().set("RoleList:"+(String) o,role);
        log.info("身份：{}",role);
        return role;
    }
}
